package com.woniu.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.LoadListByPersonalCLassDto;
import com.woniu.model.dto.PersonalClassDto;
import com.woniu.model.form.PageForm;
import com.woniu.model.form.PersonalClassAddForm;
import com.woniu.model.form.PersonalClassUpdateStateForm;
import com.woniu.model.model.YPersonalClass;
import com.woniu.model.param.CoachByPagePersonalParam;
import com.woniu.model.param.CoachByPersonalParam;
import com.woniu.model.param.CoachPagePersonalClassParam;
import com.woniu.model.param.PersonalClassAddParam;
import com.woniu.model.param.PersonalClassUpdateStateParam;
import com.woniu.service.YPersonalClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yPersonalClass")
@Api(tags = "私课的接口")
public class YPersonalClassController {

    @Resource
    private YPersonalClassService personalClassService;
    @Resource
    private RedisUtil redisUtil;


    /**
     * 删除私课
     *
     * @param ：leagueClassId：私课id
     * @return
     * @throws YogaException
     */
    @GetMapping("/deletePersonalClassState")
    @ApiOperation("删除私课")
    public Result<?> deletePersonalClassState( Integer PersonalClassId) throws YogaException {
        if (PersonalClassId == null) {
            return Result.newInstance(null).setMsg("参数异常").setCode(500);
        }
        personalClassService.deletePersonalClassState(PersonalClassId);
        return Result.newInstance(null).setMsg("删除团课状态完成").setCode(200);
    }

    /**
     * 通过教练Id查该教练的私课
     *
     * @param coachId：教练id
     * @return
     */
    @GetMapping("/CoachByListIdPersonalClass")
    @ApiOperation("通过教练Id查该教练的私课")
    public Result<?> CoachByIdPersonalClass(Integer coachId) {
        List list = personalClassService.CoachByIdPersonalClass(coachId);
        return Result.newInstance(List.class).setData(list).setMsg("查询成功").setCode(200);
    }

    /**
     * 通过token查该教练的私课
     *
     * @param token
     * @return
     */
    @GetMapping("/CoachByPageIdPersonalClass")
    @ApiOperation("通过教练token查该教练的私课")
    public Result<Page<PersonalClassDto>> CoachByPageIdPersonalClass(PageForm form, @RequestHeader String token) {
        //拿id
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        //装id
        CoachByPagePersonalParam param = BeanUtil.toBean(form, CoachByPagePersonalParam.class);
        param.setCoachId(coachId);
        Page<PersonalClassDto> page = personalClassService.CoachByPageIdPersonalClass(param);

        return Result.newPageInstance(PersonalClassDto.class).setData(page).setMsg("查询成功").setCode(200);
    }




    /**
     * 通过场馆的token查私课
     *
     * @param token
     * @return
     */
    @GetMapping("/VenueByPageIdPersonalClass")
    @ApiOperation("通过场馆的token查私课")
    public Result<Page<PersonalClassDto>> VenueByPageIdPersonalClass(PageForm form, @RequestHeader String token) {
        //拿id
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        //装id
        CoachByPersonalParam param = BeanUtil.toBean(form, CoachByPersonalParam.class);
        param.setVenueId(venueId);

        Page<PersonalClassDto> page = personalClassService.VenueByPageIdPersonalClass(param);

        return Result.newPageInstance(PersonalClassDto.class).setData(page).setMsg("查询成功").setCode(200);
    }



    /**
     * 查询私课集合
     *
     * @return
     */
    @GetMapping("/loadListPersonalClass")
    @ApiOperation("查询私课集合")
    public Result<?> loadListPersonalClass() {
        return Result.newInstance(List.class).setCode(200).setMsg("查询成功").setData(personalClassService.loadListPersonalClass());
    }




    /**
     * 查询私课集合,带教练名字查
     *
     * @return
     */
    @GetMapping("/loadListByPersonalClass")
    @ApiOperation("查询私课集合,带教练名字查")
    public Result<LoadListByPersonalCLassDto> loadListByPersonalClass(String coachName) {



        return Result.newInstance(LoadListByPersonalCLassDto.class).setCode(200).setMsg("查询成功").setData(personalClassService.loadListByPersonalClass(coachName));
    }






    /**
     * 传私教课ID查询一个私教课回去
     *
     * @param id：私教课id
     * @return
     * @throws YogaException
     */
    @GetMapping("/byIdPersonal")
    @ApiOperation("传私教课ID查询一个私教课回去")
    public Result<?> byIdPersonal(Integer id) throws YogaException {
        YPersonalClass yPersonalClass = personalClassService.byIdPersonal(id);
        return Result.newInstance(YPersonalClass.class).setCode(200).setData(yPersonalClass).setMsg("查询成功");
    }

    /**
     * 修改私课状态为可预约
     * @param
     * @return
     * @throws YogaException
     */
    @GetMapping("/updatePersonalClassState")
    @ApiOperation("修改私课状态为可预约")
    public Result<?> updatePersonalClassState(Integer personalClassId) throws YogaException {
        personalClassService.updatePersonalClassState(personalClassId);
        return Result.newInstance(null).setMsg("修改私课状态完成").setCode(200);
    }

    /**
     * 新增私课
     *  由场馆新增，传教练id
     * @param form：coachId：教练；price：价格；minute：分钟数（课程时长）；classTypeId：课程类型id
     * @return
     * @throws YogaException
     */
    @PostMapping("/addPersonalClass")
    @ApiOperation("新增私课")
    public Result<?> addPersonalClass(@RequestBody PersonalClassAddForm form) throws YogaException {


        PersonalClassAddParam param = BeanUtil.toBean(form, PersonalClassAddParam.class);

        personalClassService.addPersonalClass(param);
        return Result.newInstance(null).setCode(200).setMsg("新增成功");
    }


    /**
     * 通过token查该场馆的私课
     *
     * @param token
     * @return
     */
    @GetMapping("/CoachPagePersonalClass")
    @ApiOperation("通过token查该场馆的私课")
    public Result<Page<PersonalClassDto>> CoachPagePersonalClass(PageForm form, @RequestHeader String token) {
        //拿id
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        //装id
        CoachPagePersonalClassParam param = BeanUtil.toBean(form, CoachPagePersonalClassParam.class);
        param.setVenueId(venueId);
        Page<PersonalClassDto> page = personalClassService.CoachPagePersonalClass(param);

        return Result.newPageInstance(PersonalClassDto.class).setData(page).setMsg("查询成功").setCode(200);
    }






}

